package com.cx.mall.demo.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cx.mall.api.goods.ProductFeign;
import com.cx.mall.api.goods.SkuInfoFeign;
import com.cx.mall.common.model.goods.Product;
import com.cx.mall.common.model.goods.SkuInfo;
import com.cx.mall.common.utils.ResponseEnum;
import com.cx.mall.common.utils.ResponseVO;
import com.cx.mall.demo.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private SkuInfoFeign skuInfoFeign;
    @Override
    @SentinelResource("productPage")
    public Page page(Product product) {
        ResponseVO result = productFeign.page(product);
        if(result.getCode() != ResponseEnum.SUCCESS.getCode()){
            throw new RuntimeException(result.getMessage());
        }
        Page page = JSON.parseObject(JSON.toJSONString(result.getData()), Page.class);
        return page;
    }

    @Override
    public String queryByUserIdAndSkuId(String userId, String skuId) {
        ResponseVO result = skuInfoFeign.queryByUserIdAndSkuId(userId, skuId);
        if(result.getCode() != ResponseEnum.SUCCESS.getCode()){
            throw new RuntimeException(result.getMessage());
        }
        return result.getData().toString();
    }
}
